package com.ut.demo.component;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;

public class View extends Component implements Component.DrawTask, Component.EstimateSizeListener {

    private Paint paint;
    private int width, height;

    public View(Context context) {
        super(context);
        initParam();
    }

    public View(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initParam();
    }

    private void initParam(){
        paint = new Paint();
        setEstimateSizeListener(this);
        addDrawTask(this);
    }

    @Override
    public boolean onEstimateSize(int widthEstimateConfig, int heightEstimateConfig) {
        width = EstimateSpec.getSize(widthEstimateConfig);
        height = EstimateSpec.getSize(heightEstimateConfig);

        setEstimatedSize(
                EstimateSpec.getChildSizeWithMode(width, width, EstimateSpec.NOT_EXCEED),
                EstimateSpec.getChildSizeWithMode(height, height, EstimateSpec.NOT_EXCEED));

        return true;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(12);
        paint.setColor(new Color(0xFF333333));
        canvas.drawCircle(width/2, height/2, width/2-12, paint);
        paint.setStrokeWidth(30);
        canvas.drawPoint(new Point(width/2, height/2), paint);

    }
}
